package com.example.generator;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

public class CodeGenerator {

    public static void main(String[] args) {

        //代码生成器
        AutoGenerator mpg= new AutoGenerator();

        //全局配置
        GlobalConfig gc = new GlobalConfig();
        String basePath = System.getProperty("user.dir")+"\\ujiuye\\ujiuye-api";
        gc.setOutputDir(basePath+"/src/main/java");
        gc.setAuthor("TeacherLi");
        gc.setOpen(false);
        gc.setSwagger2(true);


        //
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setFileOverride(true);

        mpg.setGlobalConfig(gc);

        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUrl("jdbc:mysql://localhost:3306/ujiuye?characterEncoding=utf8&useSSL=false&serverTimezone=UTC");
        dsc.setUsername("root");
        dsc.setPassword("123456");

        mpg.setDataSource(dsc);

        PackageConfig pk = new PackageConfig();
        pk.setModuleName("api");
        pk.setParent("com.example");
        mpg.setPackageInfo(pk);


        String templatePath="/templates/mapper.xml.ftl";
        List<FileOutConfig> fileList = new ArrayList<>();

        fileList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return basePath+"/src/main/resources/mapper/"
                        +pk.getModuleName()+"/"
                        +tableInfo.getEntityName()+"Mapper"+ StringPool.DOT_XML;
            }
        });

        InjectionConfig cfg =new InjectionConfig() {
            @Override
            public void initMap() {

            }
        };
        cfg.setFileOutConfigList(fileList);
        mpg.setCfg(cfg);


        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);

        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setControllerMappingHyphenStyle(true);

        mpg.setStrategy(strategyConfig);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();

    }
}
